package com.cucn.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cucn.commonutils.Result;
import com.cucn.eduservice.entity.EduCourse;
import com.cucn.eduservice.entity.EduTeacher;
import com.cucn.eduservice.service.EduCourseService;
import com.cucn.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 前台讲师列表与详情前端控制器
 *
 * @author KouChaoJie
 * @since: 2021/10/12 10:01
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacherfront")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    /**
     * 讲师分页查询
     *
     * @param current 当前页
     * @param limit   每页记录数
     */
    @PostMapping("/getTeacherFrontList/{current}/{limit}")
    public Result getTeacherFrontList(@PathVariable long current, @PathVariable long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        Map<String, Object> map = teacherService.getTeacherFrontList(pageTeacher);

        //要返回分页中所有数据
        return Result.ok().data(map);
    }

    /**
     * 前台讲师详情的功能
     *
     * @param teacherId 讲师id
     */
    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public Result getTeacherFrontInfo(@PathVariable String teacherId) {
        //1.根据讲师id查询讲师基本信息
        EduTeacher eduTeacher = teacherService.getById(teacherId);

        //2.根据讲师id查询讲师教授的所有课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        List<EduCourse> courseList = courseService.list(wrapper);

        return Result.ok().data("teacher", eduTeacher).data("courseList", courseList);
    }
}
